<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="门票号" prop="detail">
      <el-input v-model="dataForm.detail" placeholder="门票号"></el-input>
    </el-form-item>
    <el-form-item label="选择用户">
           <div>
    <el-cascader style="width:85%"
      filterabl
      clearable 
      placeholder="试试搜索:用户名"
      v-model="paths"
      :options="userlist"
      :props="setting"
    ></el-cascader>
  </div>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
       userlist:[],
        setting:{
        value: "id",
        label: "name",
        },
        paths:[],
        visible: false,
        dataForm: {
          id: 0,
          address: '',
          detail: '',
          deleted: '',
          userId: ''
        },
        dataRule: {
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '门票号不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
       //获取全部用户信息
       getUserList(){
        this.$http({
          url: this.$http.adornUrl('/help/user/findAll'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0) { 
              this.userlist=data.data;
          }
        })
      }
      ,
      init (id) {
        this. getUserList();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/help/address/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.address = data.address.address
                this.dataForm.detail = data.address.detail
                this.dataForm.deleted = data.address.deleted
                this.dataForm.userId = data.address.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/help/address/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'address': this.dataForm.address,
                'detail': this.dataForm.detail,
                'deleted': this.dataForm.deleted,
                'userId': this.paths[0]
              })
            }).then(({data}) => {
              this.paths=[];
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
